package com.kl.modular.sysman.system.controller;

import com.alibaba.fastjson.JSON;
import com.kl.common.pager.PageParameter;
import com.kl.common.pager.Pager;
import com.kl.common.util.UUIDUtil;
import com.kl.modular.common.controller.BaseController;
import com.kl.modular.common.response.ResponseBase;
import com.kl.modular.sysman.system.constant.ResponsePath;
import com.kl.modular.system.dao.bo.SystemAuthResourceBo;
import com.kl.modular.system.dao.model.SystemAuthResource;
import com.kl.modular.system.service.api.ISystemAuthResourceService;
import com.kl.modular.system.service.constant.SystemConStant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 系统权限Controller
 * @author xrp_zjb
 * @date 2016/1/20 21:33
 *
 */
@Controller
public class SystemAuthResourceController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(SystemAuthResourceController.class);
	
	@Autowired
	private ISystemAuthResourceService systemAuthResourceService;


	@RequestMapping(value = "/sysman/authResource/main" , method = RequestMethod.GET)
	@RequiresPermissions("sysman:SYSTEM_AUTH_MAN:viewlist")
	public String systemAuthResourceMain(HttpServletRequest request){
		return ResponsePath.GUESTSTARPOINT_AUTH_RESOURCE_MAIN;
	}
	
	/**
	 * 权限资源列表 TO LIST
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sysman/authResource/toAuthResourceList" , method = RequestMethod.GET)
	@RequiresPermissions("sysman:SYSTEM_AUTH_MAN:viewlist")
	public String toSystemAuthResourceList(SystemAuthResourceBo systemAuthResourceBo, HttpServletRequest request){
		PageParameter pageParmeter = new PageParameter();
		pageParmeter.setPageBoolean(false);
		List<SystemAuthResource> authResourceList = systemAuthResourceService.getListAllByBo(systemAuthResourceBo, pageParmeter);
		request.setAttribute("authResourceList", JSON.toJSON(authResourceList));
		request.setAttribute("bo", systemAuthResourceBo);
		return ResponsePath.GUESTSTARPOINT_AUTH_RESOURCE_LIST;
	}
	
	/**
	 * 权限资源列表
	 * @param systemAuthResourceBo
	 * @param pageParmeter
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sysman/authResource/toAuthResourceList" , method = RequestMethod.POST)
	@RequiresPermissions("sysman:SYSTEM_AUTH_MAN:viewlist")
	public String querySystemAuthResourceList(SystemAuthResourceBo systemAuthResourceBo,PageParameter pageParmeter,HttpServletRequest request) throws Exception{
		if(systemAuthResourceBo == null){
			systemAuthResourceBo = new SystemAuthResourceBo();
		}
		if(pageParmeter == null){
			pageParmeter = new PageParameter();
		}
		if(StringUtils.isEmpty(systemAuthResourceBo.getAuthParentCode())){
			systemAuthResourceBo.setAuthParentCode("0");
		}
		pageParmeter.setPageSize(1000);
		Pager<SystemAuthResource> authResourcePager = systemAuthResourceService.getListByBo(systemAuthResourceBo, pageParmeter);
		request.setAttribute("pager", authResourcePager);
		return ResponsePath.GUESTSTARPOINT_AUTH_RESOURCE_AJAXLIST;
	}
	
	/**
	 * 全部资源列表
	 * @param systemAuthResourceBo
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/sysman/authResource/authResourceAllList", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	@RequiresPermissions(logical= Logical.OR,value={"sysman:SYSTEM_AUTH_MAN:viewlist","sysman:SYSTEM_ORGANS_MAN_SECURITY:organsPersonalNewUpdate"})
	public String querySystemAuthResourceAll(SystemAuthResourceBo systemAuthResourceBo, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (systemAuthResourceBo == null) {
			systemAuthResourceBo = new SystemAuthResourceBo();
		}
		PageParameter pageParmeter = new PageParameter();
		pageParmeter.setPageBoolean(false);
		List<SystemAuthResource> authResourceList = systemAuthResourceService.getListAllByBo(systemAuthResourceBo, pageParmeter);
		return JSON.toJSONString(authResourceList);
	}
	
	/**
	 * TO 添加权限资源
	 * 
	 * @return
	 */
	@RequestMapping(value = "/sysman/authResource/addSystemAuthResource", method = RequestMethod.GET)
	@RequiresPermissions("sysman:SYSTEM_AUTH_MAN:add")
	public String toAddSystemAuthResource(String parentCode, String moduleType, HttpServletRequest request) {
		SystemAuthResource authResource = systemAuthResourceService.getSystemAuthResourceByCode(parentCode);
		request.setAttribute("parentCode", parentCode);
		request.setAttribute("authResource", authResource);
		request.setAttribute("moduleType", moduleType);
		return ResponsePath.GUESTSTARPOINT_AUTH_RESOURCE_ADD;
	}

	/**
	 * 添加权限资源
	 * @param systemAuthResource
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sysman/authResource/addSystemAuthResource", method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:SYSTEM_AUTH_MAN:add")
	public String addSystemAuthResource(SystemAuthResource systemAuthResource, HttpServletRequest request) throws Exception{
		ResponseBase responseBase = new ResponseBase();
		responseBase.setStatus(ResponseBase.RESPONSE_STATUS_FAILED);
		// 数据有效性验证
		if(systemAuthResource == null){
			logger.info("数据验证>>添加权限资源>>systemAuthResource == null");
			responseBase.setMsg("请求数据有误");
			return JSON.toJSONString(responseBase);
		}
		if(StringUtils.isEmpty(systemAuthResource.getAuthName())){
			logger.info("数据验证>>添加权限资源>>权限资源 authName 为 空");
			responseBase.setMsg("请输入权限资源名称");
			return JSON.toJSONString(responseBase);
		}
		if(StringUtils.isEmpty(systemAuthResource.getAuthShrioCode())){
			logger.info("数据验证>>添加权限资源>>权限资源CODE为空");
			responseBase.setMsg("请输入权限CODE");
			return JSON.toJSONString(responseBase);
		}
		if(StringUtils.isEmpty(systemAuthResource.getAuthType())){
			logger.info("数据验证>>添加权限资源>>权限资源类别为空");
			responseBase.setMsg("请选择权限类别");
			return JSON.toJSONString(responseBase);
		}
		if(StringUtils.isEmpty(systemAuthResource.getAuthparentCode())){
			logger.info("数据验证>>添加权限资源>>权限资源父级code为空");
			responseBase.setMsg("请选择权限父级");
			return JSON.toJSONString(responseBase);
		}
		systemAuthResource.setAuthCode(UUIDUtil.getUniquekey());
		systemAuthResource.setEnabled(SystemConStant.DATA_ENABLEDS);
		systemAuthResource.setAuthStatus(SystemConStant.SYSTEM_AUTH_STATUS_NORMAL);
		systemAuthResourceService.addSystemAuthResource(systemAuthResource);
		responseBase.setStatus(ResponseBase.RESPONSE_STATUS_SUCCESS);
		String json = JSON.toJSONString(responseBase);
		return json;
	}
	
	/**
	 * 编辑页面
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/sysman/authResource/toChangeView" , method = RequestMethod.GET)
	@RequiresPermissions("sysman:SYSTEM_AUTH_MAN:edit")
	public String toChangView(String id,HttpServletRequest request) throws Exception{
		logger.info("权限资源编辑页面>>data:" + id);
		SystemAuthResource authResource = systemAuthResourceService.getSystemAuthResourceByCode(id);
		if(!StringUtils.equals("0", authResource.getAuthparentCode())){
			SystemAuthResource authResourceParent = systemAuthResourceService.getSystemAuthResourceByCode(authResource.getAuthparentCode());
			request.setAttribute("authResourceParent", authResourceParent);
		}
		request.setAttribute("authResource", authResource);
		return ResponsePath.GUESTSTARPOINT_AUTH_RESOURCE_EDIT;
	}
	
	
	/**
	 * 权限资源编辑
	 * @param systemAuthResource
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/sysman/authResource/upChange" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:SYSTEM_AUTH_MAN:edit")
	public String upChange(SystemAuthResource systemAuthResource) throws Exception{
		ResponseBase base = new ResponseBase();
		
		if(systemAuthResource == null){
			logger.info("数据验证>>编辑权限资源>>请求数据有误");
			base.setMsg("编辑权限资源 ,请求数据有误");
			return JSON.toJSONString(base);
		}
		
		if(StringUtils.isEmpty(systemAuthResource.getAuthCode())){
			logger.info("数据验证>>编辑权限资源>>authCode is null");
			base.setMsg("编辑权限资源 ,请求数据有误,唯一标识不能为空");
			return JSON.toJSONString(base);
		}
		
		if(StringUtils.isEmpty(systemAuthResource.getAuthName())){
			logger.info("数据验证>>编辑权限资源>>authName is null");
			base.setMsg("编辑权限资源 ,请求数据有误,权限名称不能为空");
			return JSON.toJSONString(base);
		}
		
		if(StringUtils.isEmpty(systemAuthResource.getAuthShrioCode())){
			logger.info("数据验证>>编辑权限资源>>authShrioCode is null");
			base.setMsg("编辑权限资源 ,请求数据有误,权限CODE不能为空");
			return JSON.toJSONString(base);
		}
		
		if(StringUtils.isEmpty(systemAuthResource.getAuthType())){
			logger.info("数据验证>>编辑权限资源>>authType is null");
			base.setMsg("编辑权限资源 ,请求数据有误,权限类型不能为空");
			return JSON.toJSONString(base);
		}
		
		systemAuthResourceService.updateByCode(systemAuthResource);
		base.setStatus(ResponseBase.RESPONSE_STATUS_SUCCESS);
		String jsonString = JSON.toJSONString(base);
		return jsonString;
	}
	
	/**
	 * 删除权限资源
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/sysman/authResource/delSystemAuthResource" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:SYSTEM_AUTH_MAN:delete")
	public String delSystemAuthResource(String id, HttpServletRequest request) throws Exception {
		ResponseBase base = new ResponseBase();
		
		if(StringUtils.isEmpty(id)){
			logger.info("数据验证>>删除权限资源>>id is null");
			base.setMsg("参数错误，请选择要删除的权限资源");
			return JSON.toJSONString(base);
		}
		
		String [] codestrs = id.split(",");
		
		if(codestrs == null || codestrs.length == 0){
			logger.info("数据验证>>删除权限资源>>codestrs is null");
			base.setMsg("参数错误，请选择要删除的权限资源");
			return JSON.toJSONString(base);
		}
		systemAuthResourceService.deleteByCodes(codestrs);
		
		base.setStatus(ResponseBase.RESPONSE_STATUS_SUCCESS);
		String jsonstr = JSON.toJSONString(base);
		logger.info("删除权限资源>>返回view>>data:"+jsonstr);
		return jsonstr;
	}
}
